/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//必须保证index大于0，在使用中digits[0] = 0，所以可以保证
static void staticPlusOne(int *digits, int digitsSize, int index)
{
    if (++digits[index] == 10)
    {
        digits[index] = 0;
        staticPlusOne(digits, digitsSize, --index);
    }
}

int *plusOne(int *digits, int digitsSize, int *returnSize)
{
    int *retA = malloc((digitsSize + 1) * sizeof(*retA));

    *retA = 0;
    memcpy(retA + 1, digits, digitsSize * sizeof(*retA));
    staticPlusOne(retA, digitsSize + 1, digitsSize);
    if (*retA != 0)
    {
        *returnSize = digitsSize + 1;
        return retA;
    }
    else
    {
        memcpy(retA, retA + 1, digitsSize * sizeof(*retA));
        *returnSize = digitsSize;
        return retA;
    }
}

int main()
{
    int test[3] = {9,
                   9,
                   9};
    for (int i = 0; i < 3; i++)
    {
        printf("%d,", test[i]);
    }
    printf("\n");
    int count;
    int *ret = plusOne(test, 3, &count);
    for (int i = 0; i < count; i++)
    {
        printf("%d,", ret[i]);
    }
    printf("\n");
    free(ret);
}